function uploadVideo() {
	var errorCode;
	$("#uploadErrorReport").html("Upload in progress");
	//upload the file through ajax
	$.ajax({
		url : "uploadAndConvertVideo.php",
		data : {
			idCourse : $("input[name='idCourse']").val(),
		},
		async : false,
	}).done(function(res) {
		errorCode = res;
	});

	//Check if an error is reported and display the proper error message
	$("#uploadErrorReport").html("errorcode :" + errorCode);
	switch(errorCode) {
		case -2 :
			$("#uploadErrorReport").html("Error in the database during the upload. Please try again.");
		case -1 :
			$("#uploadErrorReport").html("Error during the upload, the file is not in the right place. Please try again.");
		case 0 :
			$("#uploadErrorReport").html("Upload Successful !");
		case 4 :
			//UPLOAD_ERR_NO_FILE
			$("#uploadErrorReport").html("The specified file does not exist. Please try again.");
		case 1 :
			//UPLOAD_ERR_INI_SIZE
			$("#uploadErrorReport").html("The video is too heavy (>5Go). Try again with a lighter one.");
		case 2 :
			//UPLOAD_ERR_FORM_SIZE
			$("#uploadErrorReport").html("The video is too heavy (>5Go). Try again with a lighter one.");
		case 3 :
			//UPLOAD_ERR_PARTIAL
			$("#uploadErrorReport").html("The video was partially transfered. Please try again.");
	}
}

function initSlyScrollBar() {
	//Create the slider
	$('.frame').sly({
		horizontal : 1,
		itemNav : 'basic',
		scrollBy : 1,
		dragging : 1,
		speed : 200,
		scrollBar : $('.scrollbar'),
		mouseDragging : 1,
		activateOn : 'click',
		startAt : 2,
		dragHandle : 1
	});

	//bond hover on items
	$("li").each(hoverImg);
	
	//set the dynamic css
	$("#playPause").css("position", "absolute");
	$("#playPause").css("top",$("#videoPlayer").height());
	$("aside").css("max-height",$("#videoBlock").height());
	
}

//Show the slide in full size when hovering the miniature
function hoverImg() {
	var img = $(this).find("img");
	var id = img.attr('id');
	var src = img.attr('src');
	img.qtip({
		id : 'bigImg' + id,
		content : '<img src="' + src + '" width=640 height=360/>',
		position : {
			my : 'bottom center',
			at : 'top center',
			target : img
		},
		show : 'mouseover',
		hide : 'mouseout',

	});

	$(this).hover(function() {

		var pos = img.position();
		var top = pos.top;
		var left = pos.left;
		var addSlide = $('<button id="add" type="button" onclick="addSlide(' + "'" + id + "'" + ')"> Add</button>').css({
			'position' : 'absolute',
			'z-index' : '100000'
		})
		img.before(addSlide);
	}, function() {
		$('#add').remove();
	});
}

slidesToBeAdded = new Array();

function addSlide(id) {
		var time = document.getElementById("video").currentTime;
		var sec = parseInt(time)%60;
		var min =  Math.floor(parseInt(time)/60);
		var stringTime = ('0'+min).slice(-2)+":"+('0'+sec).slice(-2);
	$("#listAddedSlides").append('<tr id="tr' + id + '"><td>' + id + ' at '+stringTime+' <button type="button" id="remove" onclick="removeSlide(' + "'" + id + "'" + ')">Remove</button></td></tr>');

	var slide = Array(id, time);
	slidesToBeAdded.push(slide);
}

function removeSlide(id) {
	var i = findIndex(slidesToBeAdded, id);
	slidesToBeAdded.splice(i, 1);
	var tagId = "tr" + id;
	$(escjQuery(tagId)).remove();
}

function findIndex(array, id) {
	for (var i = 0; i < array.length; i++) {
		if (array[i][0] == id) {
			return i;
		}
	}
	return -1;
}

function saveAllSelectedSlides(idVideo, path) {
	var confirmString = "Are you sure you want to save all "+slidesToBeAdded.length+" slides as you configured them ?";
	var check = confirm(confirmString);
	if (check) {
		for (var i = 0; i < slidesToBeAdded.length; i++) {
			$.ajax({
				url : "saveSlide.php",
				data : {
					id : idVideo,
					slide : slidesToBeAdded[i][0],
					time : slidesToBeAdded[i][1],
					folder : path
				},
				async : false,
				dataType : "text"
			});
		}
		
		return true;
	}else{
		return false;
	}
}

function escjQuery(myid) {

	return "#" + myid.replace(/(:|\.|\[|\])/g, "\\$1");

}
